<?php

class ClienteModel extends AppModel {

	protected $_tableName = "cliente";
	
	public static function listAllToSelect($condominio_id, $selected_id=null) {
		$clienteModel = new ClienteModel();
		$clientes = $clienteModel->findAllByCondominio($condominio_id);
		$response = array();
		$response[] = array( "option.value" => 0, "option.label" => Messages::get("naoInformado") );
		foreach ($clientes as $cliente){
			$tmp = array();
			$tmp[ "option.value" ] = Encryption::encode($cliente["id"]);
			$tmp[ "option.label" ] = $cliente["nome"];
			if( $cliente["id"] == $selected_id )
				$tmp[ "option.selected" ] = "selected";
			$response[] = $tmp;
		}
		return $response;
	}
	
	public static function markInSelectArray($value,$selectArray) {
		foreach ($selectArray as $option)
			if( $option[ "option.value" ] == $value ){
				$option[ "option.selected" ] = "selected";
				break;
			}
		return $selectArray;
	}
	
	public static function convertClientePorCondominioToSelectArray($condominio_id=null, $valueSelected="") {
		$response = array();
		if( $condominio_id != null ){
			$response[] = array( "option.value" => "0", "option.label" => "Selecione um cliente" );
			$clienteModel = new ClienteModel();
			$clientes = $clienteModel->find(
				array(
					"condominio_id" => $condominio_id, // Condmonio onde possui unidade
					"cliente_id" => 0 // Não tem um cliente pai, ou seja, não é dependente
				)
			);
			foreach ($clientes as $cliente){
				$tmp = array();
				$tmp[ "option.value" ] = Encryption::encode($cliente['id']);
				if( $cliente['id'] == $valueSelected )
					$tmp[ "option.selected" ] = "selected";
				$tmp[ "option.label" ] = $cliente['nome'];
				$response[] = $tmp;
			}
		}
		return $response;
	}

	public static function createHeadersToTable() {
		$headers = array( 
					array( "th" => "Nome" ),
					array( "th" => "Status" ),
					array( "th" => "Telefone" ),
					array( "th" => "Nascimento" ),
					array( "th" => "Ações")
				);
		return $headers;
	}
	
	public static function listDependentesToTableByCondominioAndCliente($condominio_id, $cliente_id, $isDependente=false){
		$clienteModel = new ClienteModel();
		$results = $clienteModel->find(
			array( 
				"condominio_id" => $condominio_id, 
				"cliente_id" => $cliente_id
			)
		);
		$response = array();
		foreach ( $results as $result ){
			$tmp = array();
			$tmp[ "id" ] = Encryption::encode( $result["id"] );
			$tmp[ "nome" ] = $result[ "nome" ];
			$tmp[ "status" ] = Status::convertToString($result[ "ativo" ]);
			$tmp[ "telefone" ] = Mask::maskNumber( $result[ "telefone" ], Mask::$TELEFONE);
			$tmp[ "data.nascimento" ] = DateUtil::convertDate($result[ "data_nascimento" ], DateUtil::$EN_US, DateUtil::$PT_BR);
			$response[] = $tmp;
		}
		return $response;
	}
	
	public static function listClientesToTableByCondominio($condominio_id, $isDependente=false){
		$clienteModel = new ClienteModel();
		$results = $clienteModel->find(
			array( 
				"condominio_id" => $condominio_id, 
				"cliente_id" => 0
			)
		);
		$response = array();
		foreach ( $results as $result ){
			$tmp = array();
			$tmp[ "id" ] = Encryption::encode( $result["id"] );
			$tmp[ "nome" ] = $result[ "nome" ];
			$tmp[ "status" ] = Status::convertToString($result[ "ativo" ]);
			$tmp[ "telefone" ] = Mask::maskNumber( $result[ "telefone" ], Mask::$TELEFONE);
			$tmp[ "data.nascimento" ] = DateUtil::convertDate($result[ "data_nascimento" ], DateUtil::$EN_US, DateUtil::$PT_BR);
			$response[] = $tmp;
		}
		return $response;
	}
	
}

?>